package com.ecgsystem.graph;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.TimeSeries;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Paint.Align;

public class LineGraph {

	private GraphicalView view;
	
	private TimeSeries dataset; 
	private TimeSeries dataset2; 
	private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
	
	private XYSeriesRenderer renderer = new XYSeriesRenderer(); // This will be used to customize line 1
	private XYSeriesRenderer renderer2 = new XYSeriesRenderer();
	
	private  XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); // Holds a collection of XYSeriesRenderer and customizes the graph
	
	public TimeSeries getDataset() {
		return dataset;
	}

	public void setDataset(TimeSeries dataset) {
		this.dataset = dataset;
	}

	public XYMultipleSeriesRenderer getmRenderer() {
		return mRenderer;
	}

	public void setmRenderer(XYMultipleSeriesRenderer mRenderer) {
		this.mRenderer = mRenderer;
	}

	public LineGraph(String title)
	{
		// Add single dataset to multiple dataset
		dataset = new TimeSeries("");
		dataset2 = new TimeSeries("");
		mDataset.addSeries(0,dataset);
		mDataset.addSeries(1,dataset2);
		
		// Customization time for line 1!
		renderer.setColor(Color.GREEN);
		renderer.setPointStyle(PointStyle.POINT);
		renderer.setLineWidth(2);
		renderer.setFillPoints(false);
		
		renderer2.setColor(Color.RED);
		renderer2.setPointStyle(PointStyle.POINT);
		renderer2.setLineWidth(2);
		renderer2.setFillPoints(false);
		
		// Disable Zoom
		mRenderer.setZoomButtonsVisible(false);
		mRenderer.setZoomEnabled(true);
		mRenderer.setXTitle("Time(ms)");
		mRenderer.setYTitle("Vol(mV)");
		mRenderer.setLabelsColor(Color.parseColor("#EBE4E4"));
		mRenderer.setAxisTitleTextSize(15);
		mRenderer.setLabelsTextSize(20);
		// Add single renderer to multiple renderer
		
		mRenderer.addSeriesRenderer(0, renderer);	
		mRenderer.addSeriesRenderer(1, renderer2);
		
		mRenderer.setChartTitle(title);
		mRenderer.setChartTitleTextSize(20);

		mRenderer.setGridColor(Color.parseColor("#FAA0A1"));
		mRenderer.setShowGrid(true);
		mRenderer.setShowLegend(false);
		mRenderer.setShowLabels(true);
	}
	
	public GraphicalView getView(Context context) 
	{
		view =  ChartFactory.getLineChartView(context, mDataset, mRenderer);
		return view;
	}
	
	public void addNewPoints(MyPoint p)
	{
		dataset.add(p.getX(), p.getY());
	}
	
	public void clearSeries2(){
		this.dataset2.clear();
	}
	
	public void displayRED(int start, int end){
		for(int i = start; i <= end;  i= i + 2){
			this.dataset2.add(dataset.getX(i / 2), dataset.getY(i / 2));
		}
	}
}
